package e_routing_topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;
import utils.RabbitMQUtils;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/06/23:41
 * @Description: 生产者
 */
public class Producer {

  @Test
  public void sendMessage() throws  Exception{
    Connection connection = RabbitMQUtils.getConnection();
    //创建通道
    Channel channel = connection.createChannel();
    //设置交换机
    channel.exchangeDeclare("topic", BuiltinExchangeType.TOPIC);
    //向交换机发消息 发了四条消息，分别指定了路由key
    channel.basicPublish("topic","user.insert",null,"我是一个routingKey-Topic类型的消息-user.insert".getBytes());
    channel.basicPublish("topic","user.insert.a",null,"我是一个routingKey-Topic类型的消息-user.insert.a".getBytes());
    channel.basicPublish("topic","user.update.b",null,"我是一个routingKey-Topic类型的消息-user.update.b".getBytes());
    channel.basicPublish("topic","user",null,"我是一个routingKey-Topic类型的消息-user".getBytes());
    //关闭
    RabbitMQUtils.closeChannelAndConnection(channel,connection);
    System.out.println("发送消息成功");
  }

}
